﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class Consulate : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0]);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);

        base.InitializeCulture();
    }

    private bool CheckServerObject()
    {
        bool result = false;

        if (Session["ServerModule"] != null)
        {
            mServerObject = (ServerObject)Session["ServerModule"];
        }

        if (mServerObject != null)
        {
            result = true;
        }

        return result;
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        ConsulatePanel.Visible = false;
        addNewConsulatPanel.Visible = false;
        editConsulatPanel.Visible = false;
      
        #endregion Hide panels

        try
        {
            if (CheckServerObject())
            {
                #region Get Panel Name

                switch (panelName)
                {
                    #region Consulate
                    case "ConsulatePanel":
                        ConsulatePanel.Visible = true;
                        mServerObject.InfoText(this, "Lista Consulatelor");

                        DataTable consulateList = mServerObject.GetConsulatList();
                        consulatListGridView.DataSource = consulateList;
                        consulatListGridView.DataBind();

                        break;

                    case "addNewConsulatPanel":
                        addNewConsulatPanel.Visible = true;
                        DataTable tariList = mServerObject.GetCountryList();
                        mServerObject.fillSelector(addNewConsulatTaraDDL, tariList, "DenumireJudet", "idJudet");
                        mServerObject.InfoText(this, "Adaugarea unui nou Consulat");
                        break;

                    case "editConsulatPanel":
                        editConsulatPanel.Visible = true;
                        DataTable tari = mServerObject.GetCountryList();
                        mServerObject.fillSelector(editConsulatTaraDDL, tari, "DenumireJudet", "idJudet");

                        mServerObject.InfoText(this, "Editarea Consulatului");
                        break;


                    #endregion Consulate

                    default:
                        break;
                }

                #endregion Get Panel Name
            }
        }
        catch (Exception exception)
        {
            mServerObject.LogFatal(exception.Message);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "logout":
                    Session["UserData"] = null;
                    Session["ServerModule"] = null;
                    Response.Redirect("Default.aspx", true);
                    break;
                default:
                    break;
            }
        }

        string areaGUID = string.Empty;
        if (Request["area"] != null)
        {
            areaGUID = Request["area"].ToString();
        }

        DataTable userData = (DataTable)Session["UserData"];

        #region Display Panels depending of area
        if (userData != null && userData.Rows.Count > 0)
        {
            if (CheckServerObject())
            {
                if (!IsPostBack)
                {
                    string areaName = areaGUID; //mServerObject.GetAreaName(areaGUID);
                    // area for each of administration entry points
                    switch (areaName)
                    {
                        case "consulatList":
                            ShowPanel(ConsulatePanel.ID);
                            break;                      

                        default:
                            ShowPanel(string.Empty);
                            mServerObject.InfoText(this, "Please select area from left panel menu");
                            break;
                    }
                }
            }
        }

        #endregion Display Panels depending of area
    }

    #region Consulate Panel Events

    protected void consulatListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        consulatListGridView.PageIndex = e.NewPageIndex;
        ShowPanel(ConsulatePanel.ID);
    }

    protected void consulatListEditButton_Click(object sender, GridViewEditEventArgs e)
    {
        int rowIndex = e.NewEditIndex;

        try
        {
            string consulatIDString = consulatListGridView.Rows[rowIndex].Cells[0].Text;
            string denumirea = consulatListGridView.Rows[rowIndex].Cells[3].Text;
            string adresa = consulatListGridView.Rows[rowIndex].Cells[4].Text;
            string telefon = consulatListGridView.Rows[rowIndex].Cells[5].Text;
            string fax = consulatListGridView.Rows[rowIndex].Cells[6].Text;
            string email = consulatListGridView.Rows[rowIndex].Cells[7].Text;
            string webPage = consulatListGridView.Rows[rowIndex].Cells[8].Text;

            editConsulatLabelID.Text = consulatIDString.Replace("&nbsp;", "");
            editConsulatDenumireTextBox.Text = denumirea.Replace("&nbsp;", "");
            editConsulatAdresaTextBox.Text = adresa.Replace("&nbsp;", "");
            editConsulatTelefonTextBox.Text = telefon.Replace("&nbsp;", "");
            editConsulatFaxTextBox.Text = fax.Replace("&nbsp;", "");
            editConsulatEmailTextBox.Text = email.Replace("&nbsp;", "");
            editConsulatWebTextBox.Text = webPage.Replace("&nbsp;", "");

            editConsulatTaraDDL.SelectedValue = consulatListGridView.Rows[rowIndex].Cells[1].Text;
        }
        catch
        {
        }

        ShowPanel(editConsulatPanel.ID);
    }

    protected void addNewConsualtButton_Click(object sender, EventArgs e)
    {
        ShowPanel(addNewConsulatPanel.ID);
    }

    protected void addNewConsulatSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            try
            {
                int idTara = 0;
                int.TryParse(addNewConsulatTaraDDL.SelectedValue, out idTara);

                string denumirea = addNewConsulatDenumireaTextBox.Text;
                string adresa = addNewConsulatAdresaTextBox.Text;
                string telefon = addNewConsulatTelefonTextBox.Text.Trim();
                string fax = addNewConsulatFaxTextBox.Text.Trim();
                string email = addNewConsulatEmailTextBox.Text.Trim();
                string webPage = addNewConsulatWebTextBox.Text.Trim();

                if (mServerObject.AddNewConsulat(idTara, denumirea, adresa, telefon, email, fax, webPage))
                {
                    mServerObject.InfoText(this, "For " + denumirea + " all data succesiful saved!");
                }
                else
                {
                    mServerObject.InfoText(this, "For " + denumirea + " Records NOT SAVED !");
                }
            }
            catch (Exception ex)
            {
                mServerObject.InfoText(this, "Unable to save CONSULAT" + ex.Message);
            }
        }

        ShowPanel(ConsulatePanel.ID);
    }

    protected void addNewConsulatCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(ConsulatePanel.ID);
    }

    protected void editConsulatSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            try
            {
                string stringIdConsulat = editConsulatLabelID.Text;
                int idConsulat = 0;
                int.TryParse(stringIdConsulat, out idConsulat);

                int idTara = 0;
                int.TryParse(editConsulatTaraDDL.SelectedValue, out idTara);

                string denumirea = editConsulatDenumireTextBox.Text;
                string adresa = editConsulatAdresaTextBox.Text;
                string telefon = editConsulatTelefonTextBox.Text.Trim();
                string fax = editConsulatFaxTextBox.Text.Trim();
                string email = editConsulatEmailTextBox.Text.Trim();
                string webPage = editConsulatWebTextBox.Text.Trim();

                if (idConsulat != 0 && mServerObject.UpdateConsulat(idConsulat, idTara, denumirea, adresa, telefon, email, fax, webPage))
                {
                    mServerObject.InfoText(this, "For " + denumirea + " all data succesiful saved!");
                }
                else
                {
                    mServerObject.InfoText(this, "For " + denumirea + " Records NOT SAVED !");
                }
            }
            catch (Exception ex)
            {
                mServerObject.InfoText(this, "Unable to save CONSULAT" + ex.Message);
            }
        }

        ShowPanel(ConsulatePanel.ID);
    }

    protected void editConsulatCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(ConsulatePanel.ID);
    }

    #endregion Consulate Panel Events
}